package com.wiz.web.manager;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.wiz.common.Constant;
import com.wiz.common.util.DecimalUtil;
import com.wiz.web.dao.mapper.MemberMapper;
import com.wiz.web.vo.WxModelMessageVO;
import com.wiz.web.vo.criteria.MemberCriteria;
import com.wiz.web.vo.to.AccountChargeHistoryTO;
import com.wiz.web.vo.to.MemberTO;
import com.wiz.web.vo.to.SmsChargeTO;
import com.wiz.web.vo.to.SmsFuellingOrderTO;

/**
 * 短信管理器
 * @author Hakeny
 */
@Service("smsManager")
public class SMSManager implements Constant{
	private transient final Logger log = LoggerFactory.getLogger(getClass());
	
	//满足linux环境下需求
	private Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
	@Resource
	private HttpManager httpManager;
	@Resource
	private MemberMapper memberMapper;
	
	/**
	 * 交易消息
	 * @param vo
	 * @param memberId
	 * @throws Exception
	 */
	public void consumpMessage(WxModelMessageVO vo){
		if(StringUtils.isBlank(vo.getPhoneNum())){
			return;
		}
		List<NameValuePair> formparams = new ArrayList<NameValuePair>();
		formparams.add(new BasicNameValuePair("HQId", vo.getHqId()));
		formparams.add(new BasicNameValuePair("stationId", vo.getStationId()));
		formparams.add(new BasicNameValuePair("phoneNum", vo.getPhoneNum()));
		SmsFuellingOrderTO sms = new SmsFuellingOrderTO();
		sms.setCreatedTime(vo.getCreatedTime());
		sms.setStationName(vo.getStationName());
		sms.setPRName(vo.getPRName());
		sms.setVol(vo.getVol());
		sms.setAmount(vo.getAmount());
		sms.setDiscountAmount(vo.getDiscountAmount());
		sms.setRealincomeAmount(vo.getRealincomeAmount());
		sms.setBalanceAmount(vo.getBalanceAmount());
		sms.setPaidTypeName(vo.getPaidTypeName());
		formparams.add(new BasicNameValuePair("msg", gson.toJson(sms)));
		formparams.add(new BasicNameValuePair("type", "1"));
		try {
			httpManager.getHttpResponseByPost(CLOUD_REMOTE_SMS, SEND_CODE_URL, formparams);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 充值消息
	 * @param vo
	 * @param memberId
	 * @throws Exception
	 */
	public void chargeMessage(AccountChargeHistoryTO to){
		MemberCriteria criteria = new MemberCriteria();
		criteria.setMemberId(to.getMemberId());
		criteria.setHqId(to.getHqId());
		MemberTO memberTO = memberMapper.getMemberInfo(criteria);
		if(StringUtils.isBlank(memberTO.getPhoneNum())){
			return;
		}
		List<NameValuePair> formparams = new ArrayList<NameValuePair>();
		formparams.add(new BasicNameValuePair("HQId", to.getHqId()));
		formparams.add(new BasicNameValuePair("stationId", to.getStationId()));
		formparams.add(new BasicNameValuePair("phoneNum", memberTO.getPhoneNum()));
		SmsChargeTO sms = new SmsChargeTO();
		sms.setHQName(to.getHqName());
		sms.setChargeTime(to.getCreatedTime());
		Double totalAmount = DecimalUtil.add(to.getGiftAmount(), to.getAmount());
		sms.setTotalAmount(totalAmount.toString());
		sms.setGiftAmount(to.getGiftAmount().toString());
		sms.setBalanceAmount(memberTO.getAmount().toString());
		formparams.add(new BasicNameValuePair("msg", gson.toJson(sms)));
		formparams.add(new BasicNameValuePair("type", "2"));
		try {
			httpManager.getHttpResponseByPost(CLOUD_REMOTE_SMS, SEND_CODE_URL, formparams);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
